<?php
/**
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/4/25
 * Time: 14:56
 */
class DingshiXunChaModel extends BaseModel
{

    public $_field_arr = [
        'id',
        'zhuyuan_id',
        'user_number',
        'user_name',
        'dingshi_pici',
        'hulijibie',
        'zhixing_content',
        'zhixing_state',
        'zhixing_time',
    ];
    public function getDingshiXuchaData($zhuyuan_id,$user_department,$data,$hulijibie,$modify_time = '')
    {

        $query = DB::select("id,zhuyuan_id,user_number,user_name,dingshi_pici,hulijibie,zhixing_content,zhixing_state,zhixing_time")
            ->from('dingshi_xunshi_zhixing')
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->and_where('hulijibie', '=', $hulijibie);
        $zhuayun_bingren = CommonModel::getZhuyuanIds($user_department,$this->yiyuan_id,$zhuyuan_id);
        $dingshi_pici = $this->getLastestZhouQi($hulijibie,$zhuayun_bingren);
        if($dingshi_pici)
        {
            $query->where('dingshi_pici', '=', $dingshi_pici);
        }
        if($modify_time)
            $query->where('modify_time', '>=', $modify_time);
        if (!empty($data))
            $query->where('id', 'NOT IN', $data);
        if($zhuayun_bingren)
            $query->where('zhuyuan_id', 'IN', $zhuayun_bingren);
        $tizheng_data = $query ->execute()->as_array();
        return $tizheng_data;
    }

    public function getLastestZhouQi($hulijibie,$zhuayun_bingren)
    {
        $res = DB::select("dingshi_pici")
            ->from("dingshi_xunshi_zhixing")
            ->where("hulijibie","=",$hulijibie)
            ->and_where("zhuyuan_id","in",$zhuayun_bingren)
            ->group_by("dingshi_pici")
            ->order_by("modify_time","DESC")
            ->execute()
            ->get("dingshi_pici","");
        return $res;

    }


}